﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using ExtremeML.Core;
using ExtremeML.Sdk.Vml;

namespace ExtremeML.Packaging
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class VmlDrawingPartWrapper
	{
		#region Custom Properties

		/// <summary>
		/// Gets the containing package as a <see cref="SpreadsheetDocumentWrapper"/>.
		/// </summary>
		public SpreadsheetDocumentWrapper Spreadsheet
		{
			get { return (SpreadsheetDocumentWrapper)Package; }
		}

		/// <summary>
		/// Gets the <see cref="VmlDrawing"/> root element.
		/// </summary>
		public VmlDrawing Drawing { get; private set; } 

		#endregion
		#region Custom Methods


		#endregion
		#region Overrides

		/// <summary>
		/// Implements custom initialization functionality.
		/// </summary>
		protected override void OnCustomInitializing()
		{
			Drawing = new VmlDrawing();
		}

		/// <summary>
		/// Implements custom population functionality.
		/// </summary>
		protected override void OnCustomDecomposing()
		{
			using (var stream = Wrapped.GetStream(FileMode.Open, FileAccess.Read))
			using (var reader = XmlReader.Create(stream))
			{
				Drawing = new VmlDrawing(XDocument.Load(reader));
			}
		}

		/// <summary>
		/// Implements custom persistence functionality.
		/// </summary>
		protected override void OnCustomRecomposed(
			VmlDrawingPart wrapped)
		{
			Drawing.Save(wrapped);
		}

		/// <summary>
		/// Returns the set of <see cref="IWrapper"/> instances defined as custom properties.
		/// </summary>
		protected override IEnumerable<IWrapper> GetCustomWrapperMembers()
		{
			yield return Drawing;
		}

		#endregion
		#region Implementation Methods


		#endregion
		#region Static Methods

		/// <summary>
		/// Returns a <see cref="VmlDrawingPart"/> populated with custom default content.
		/// </summary>
		/// <param name="container">The parent <see cref="OpenXmlPartContainer"/> instance.</param>
        /// <param name="args">A parameter array of <see cref="object"/> containing member initialization arguments.</param>
        /// <returns>A new instance of <see cref="VmlDrawingPart"/>.</returns>
		internal static new VmlDrawingPart CreateCustomDefaultPart(
			OpenXmlPartContainer container,
            params object[] args)
		{
			var part = container.AddNewPart<VmlDrawingPart>();

			new VmlDrawing(
				new CommentShapeLayout(),
				new CommentShapeType()
			).Save(part);		

			return part;
		}

		#endregion
	}
}
